package com.allm.ai.api.controller;

import java.util.Map;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.allm.ai.api.dto.UserInfo;
import com.allm.ai.api.service.UserService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 用户认证API控制器
 */
@Slf4j
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final UserService userService;

    /**
     * 验证用户身份 - 通过拦截器自动验证token
     * 如果到达这里说明token验证通过，返回当前用户信息
     */
    @GetMapping("/verify")
    public ResponseEntity<Map<String, Object>> verifyUser() {
        try {
            UserInfo currentUser = userService.getCurrentUser();
            if (currentUser != null) {
                return ResponseEntity.ok(Map.of(
                    "success", true,
                    "message", "用户认证成功",
                    "user", Map.of(
                        "id", currentUser.getId(),
                        "username", currentUser.getUsername(),
                        "nickname", currentUser.getNickname(),
                        "email", currentUser.getEmail(),
                        "picture", currentUser.getPicture()
                    )
                ));
            } else {
                return ResponseEntity.ok(Map.of(
                    "success", false,
                    "message", "用户信息获取失败"
                ));
            }
        } catch (Exception e) {
            log.error("用户认证验证失败", e);
            return ResponseEntity.ok(Map.of(
                "success", false,
                "message", "用户认证失败"
            ));
        }
    }
}
